package com.tony.blog.api.utils;

import org.springframework.validation.BindingResult;

/**
 * Created with IntelliJ IDEA.
 *
 * @Title: ValidatorUtil
 * @Auther: 皮蛋布丁
 * @Date: 2021/07/17/23:12
 * @Description: 请求参数统一校验工具
 */
public class ValidatorUtil {

    public static String checkResult(BindingResult result) {
        StringBuilder sb = new StringBuilder("");
        if (result != null && result.hasErrors()) {
            /*List<ObjectError> errors=result.getAllErrors();
            for (ObjectError error:errors){
                sb.append(error.getDefaultMessage()).append("\n");
            }*/

            //Java8 stream写法(ALT+Shift+Enter(new Consumer)->Lambda)
            result.getAllErrors().stream().forEach(error -> sb.append(error.getDefaultMessage()).append("\n"));
        }
        return sb.toString();
    }
}
